<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" inline label-width="68px">
      <el-form-item label="柜机名称" prop="cabinetId">
        <el-select
          v-model="queryParams.cabinetId"
          placeholder="请选择柜机名称"
          filterable
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="item in cabinetList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="上报时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
          重置
        </el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="序号" type="index" width="80" align="center" />
      <el-table-column label="柜机名称" align="center">
        <template slot-scope="scope">
          {{ getCabinetName(scope.row.cabinetId) }}
        </template>
      </el-table-column>
      <el-table-column label="温度" align="center">
        <template slot-scope="scope"> {{ scope.row.temperature || "-" }}°C </template>
      </el-table-column>
      <el-table-column label="湿度" align="center" prop="humidity">
        <template slot-scope="scope"> {{ scope.row.humidity || "-" }}%RH </template>
      </el-table-column>
      <el-table-column label="PLC状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0" style="color: red">离线</span>
          <span v-if="scope.row.status == 1" style="color: green">在线</span>
        </template>
      </el-table-column>
      <el-table-column label="温湿度状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.sensorStatus == 0" style="color: red">离线</span>
          <span v-if="scope.row.sensorStatus == 1" style="color: green">在线</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="打印机状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.printerStatus == 0" style="color: red">离线</span>
          <span v-if="scope.row.printerStatus == 1" style="color: green">在线</span>
        </template>
      </el-table-column> -->
      <el-table-column label="上报时间" align="center" prop="reportTime" width="180" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listCabinet } from "@/api/mc/cabinet";
import { listHeartbeat } from "@/api/mc/heartbeat";

export default {
  name: "Heartbeat",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 日期范围
      dateRange: [],
      // 心跳记录表格数据
      dataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cabinetId: null,
        reportBeginTime: null,
        reportEndTime: null,
      },
      cabinetList: [], // 柜机列表
    };
  },
  created() {
    this.getCabinetList();
    this.getList();
  },
  methods: {
    /** 查询心跳记录列表 */
    getList() {
      this.loading = true;
      let params = { ...this.queryParams };
      if (this.dateRange && this.dateRange.length > 0) {
        params.reportBeginTime = this.dateRange[0];
        params.reportEndTime = this.dateRange[1];
      } else {
        params.reportBeginTime = null;
        params.reportEndTime = null;
      }
      listHeartbeat(params)
        .then((res) => {
          this.dataList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 查询柜机列表 */
    getCabinetList() {
      listCabinet({ pageNum: 1, pageSize: 1000 }).then((res) => {
        this.cabinetList = res.rows;
      });
    },
    // 获取柜机名称
    getCabinetName(id) {
      let row = this.cabinetList.find((item) => item.id == id);
      return row ? row.name : "";
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
